package com.cndw.kungfu.controller;

import java.util.HashMap;
import java.util.Map;

import com.cndw.kungfu.comm.AccessFlag;
import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsChat;
import com.cndw.kungfu.config.ConstantsCost;
import com.cndw.kungfu.config.ConstantsDayIntKey;
import com.cndw.kungfu.config.ConstantsFlag;
import com.cndw.kungfu.config.ConstantsLevel;
import com.cndw.kungfu.config.ConstantsLog;
import com.cndw.kungfu.config.ConstantsModel;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.LanguageLib;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.activity.domain.BalefileStaticeData;
import com.cndw.kungfu.model.operation.WinneCenter;
import com.cndw.kungfu.model.pvp.api.CommApiClient;
import com.cndw.kungfu.model.sys.ChatMain;
import com.cndw.kungfu.model.sys.LogServer;
import com.cndw.rpg.annotation.Controller;
import com.cndw.rpg.framework.ResponseMessage;
import com.cndw.rpg.framework.Session;

/**
 * 一些集体趣味玩法汇总
 * 
 * @author fantadust
 * 
 */
@AccessFlag(ConstantsFlag.meetWinne)
@Controller
public class MeetController {

	/**
	 * 显示敬酒的界面
	 * 
	 * @param session
	 * @return
	 */
	public ResponseMessage winneShow(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		int meToday = player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.winne);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("meToday", meToday);
		map.put("gotExp", WinneCenter.getBaseExp(player.getLevel()) * 9);
		return ResponseMessage.getSuccMessage(map);
	}

	public ResponseMessage winneOpen(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (player.getLevel() < ConstantsLevel.lv40winne) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.level", ConstantsLevel.lv40winne));
		}
		if (!TimeProcesser.inTimePeriod(ConstantsModel.winneTimeArea[0], ConstantsModel.winneTimeArea[1])) {
			return null;
		}
		int meToday = player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.winne);
		int costGold = meToday * 10 + ConstantsModel.winneCostGold;
		if (meToday >= 30) {
			// return
			// ResponseMessage.ticpErrMsg(LanguageLib.get("meet.winne.maxtimes",30));
		}
		meToday++;
		if (player.getGoldAll() < costGold) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		player.alterGoldAll(-costGold);
		player.getDataInfo().getEveryDay().intDataSet(ConstantsDayIntKey.winne, meToday);
		int gotExp = WinneCenter.getBaseExp(player.getLevel()) * 9;
		if (meToday == 1) {
			gotExp *= 2;
		}
		player.alterExp(gotExp);
		player.commitSync();
		player.getDataInfo().commitSync();

		WinneCenter.open(player);

		LogServer.costEventLog(session.getUid(), costGold, ConstantsCost.winneOpen);
		LogServer.recordLog(player, ConstantsLog.activity, 90411, costGold, gotExp);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());
		map.put("exp", player.getExp());
		map.put("gotExp", gotExp);

		String message = LanguageLib.get("meet.winne.chat");
		ChatMain.commMessage(player, message, ConstantsChat.xtWinne, null);

		return ResponseMessage.getSuccMessage(map);
	}

	/**
	 * 敬酒
	 * 
	 * @param session
	 * @param type
	 *            0 自动点击 1 免费 2 1000银子 3 1元宝
	 * @return
	 */
	public ResponseMessage winneOffer(Session session, int signId, int type, long openId) {
		if (type < 0 || type > 3) {
			return null;
		}
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (player.getLevel() < ConstantsLevel.lv40winne) {
			return null;
		}
		int costAsset = 0, costGold = 0;
		if (type == 2) {
			costAsset = 1000;
		} else if (type == 3) {
			costGold = 1;
		}
		if (player.getAsset() < costAsset) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.asset"));
		}
		if (player.getGoldAll() < costGold) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.gold"));
		}
		boolean suc = WinneCenter.isOffer(signId, session.getUid());
		if (!suc) {
			return null;
		}
		if (costAsset > 0) {
			player.alterAsset(-costAsset);
		}
		if (costGold > 0) {
			player.alterGoldAll(-costGold);
			LogServer.costEventLog(session.getUid(), costGold, ConstantsCost.winneOffer);
		}
		int gotExp = WinneCenter.getOfferExp(player.getLevel(), type);
		player.alterExp(gotExp);

		player.commitSync();
		LogServer.recordLog(player, ConstantsLog.activity, 90412, type, costAsset, costGold, gotExp);
		if (type == 0) {
			Player target = Context.getPlayerServer().getByPlayerId(openId);
			if (target != null) {
				Context.getMessageServer().recordEvent(session.getUid(), LanguageLib.get("meet.winne.title"),
						LanguageLib.get("meet.winne.content", target.getPlayerName(), gotExp), Constants.eventTypeNormal);
			}
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("asset", player.getAsset());
		map.put("gold", player.getGold());
		map.put("goldBing", player.getGoldBing());
		map.put("gotExp", gotExp);
		map.put("exp", player.getExp());
		return ResponseMessage.getSuccMessage(map);
	}

	public ResponseMessage balefire(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		int fht = player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.fht);
		int state = CommApiClient.balefireState();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("fht", fht);
		map.put("state", state);
		return ResponseMessage.getSuccMessage(map);
	}

	public ResponseMessage balefireDo(Session session) {
		Player player = Context.getPlayerServer().getByPlayerId(session.getUid());
		if (player.getLevel() < ConstantsLevel.lv30balefire) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.level", ConstantsLevel.lv30balefire));
		}
		int fht = player.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.fht);
		if (fht >= 3) {
			return null;
		}
		int ret = CommApiClient.balefireSend(session.getUid(), player.getPlayerName());
		if (ret == -1) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("meet.fire.close"));
		} else if (ret < -1) {
			return ResponseMessage.ticpErrMsg(LanguageLib.get("comm.err.sys"));
		}
		fht++;
		player.getDataInfo().getEveryDay().intDataSet(ConstantsDayIntKey.fht, fht);
		BalefileStaticeData.currentAdd(session.getUid(), ret);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("num", ret);
		map.put("fht", fht);
		LogServer.recordLog(player, ConstantsLog.actNew15, 150102, ret);
		return ResponseMessage.getSuccMessage(map);
	}

}
